/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.fragment.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import cn.kcrx.autoweightapp.R
import cn.kcrx.autoweightapp.databinding.AdapterItemCashweightsettingBinding
import cn.kcrx.autoweightapp.models.NewCashBox
import cn.kcrx.autoweightapp.models.NewCashBoxSetting

class CashWeightSettingAdapter:ListAdapter<NewCashBox, CashWeightSettingAdapter.NewCashBoxViewHolder>(NewCashBoxCallBack()){

    inner class NewCashBoxViewHolder(val binding: AdapterItemCashweightsettingBinding)
        :RecyclerView.ViewHolder(binding.root) {

         fun bind(newItem: NewCashBox,i:Int){
             binding.tvCashname.text=NewCashBoxSetting.getCashNameTxt(newItem.cashName.ordinal)
             binding.etCashweight.setText(newItem.weight.toString())
             binding.etCashweightRange.setText(newItem.weightrange.toString())
             binding.etCashweight.setOnFocusChangeListener { view, b ->
                 if(!b){
                     NewCashBoxSetting.settinglist[i].weight=binding.etCashweight.text.toString().toFloat()
                     NewCashBoxSetting.saveSetting()
                 }
             }
             binding.etCashweightRange.setOnFocusChangeListener { view, b ->
                 if(!b){
                     NewCashBoxSetting.settinglist[i].weightrange=binding.etCashweightRange.text.toString().toFloat()
                     NewCashBoxSetting.saveSetting()
                 }
             }

         }
    }
     class NewCashBoxCallBack:DiffUtil.ItemCallback<NewCashBox>() {
        override fun areItemsTheSame(oldItem: NewCashBox, newItem: NewCashBox): Boolean {
            return oldItem.cashName==newItem.cashName
        }
        override fun areContentsTheSame(oldItem: NewCashBox, newItem: NewCashBox): Boolean {
          return oldItem==newItem
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewCashBoxViewHolder {
     return NewCashBoxViewHolder(
           AdapterItemCashweightsettingBinding.inflate(
           LayoutInflater.from(parent.context),
               parent,
               false
         )
     )
    }

    override fun onBindViewHolder(holder: NewCashBoxViewHolder, position: Int) {
        when(position){
            0->   holder.binding.lineTatol.setBackgroundResource(R.color.cash100)
            1->   holder.binding.lineTatol.setBackgroundResource(R.color.cash50)
            2->   holder.binding.lineTatol.setBackgroundResource(R.color.cash20)
            3->   holder.binding.lineTatol.setBackgroundResource(R.color.cash10)
            4->   holder.binding.lineTatol.setBackgroundResource(R.color.cash5)
            5->   holder.binding.lineTatol.setBackgroundResource(R.color.cash1)
        }
        holder.bind(getItem(position),position)
    }
}